package databaseConnect;

import java.util.Vector;

public class ScenarioConfigurationRequestProcessor {
	
	//Methods
	public static int getLastRowIDFromScenarioDescriptionTable(){
		Vector<Vector<String>> temp = AccessConnect.executeSelect("SELECT MAX(ID)FROM ScenarioDescriptions");
		try {
			if (temp != null){
				return Integer.parseInt(temp.firstElement().firstElement());	
			}
			return -1;
		} catch (NumberFormatException e) {
			return -1;
		}
	}
	
	public static Vector<Vector<String>> getFirstRowFromScenarioDescriptionTable(){
		return AccessConnect.executeSelect("SELECT * FROM ScenarioDescriptions WHERE ID=(SELECT MIN(ID) FROM ScenarioDescriptions)");
	}
	
	
	public static Vector<Vector<String>> getRowWithIDFromScenarioDescriptionTable(int ID){
		return AccessConnect.executeSelect("SELECT * FROM ScenarioDescriptions WHERE ID="+ ID + "");		
	}
	public static Vector<Vector<String>> getScenarioParameterRows (){
		Vector<String> temp = AccessConnect.getColumnNamesForSelect("SELECT * FROM ScenarioDescriptions");
		Vector<Vector<String>> templateValues = getFirstRowFromScenarioDescriptionTable();
		Vector<Vector<String>> pivot = new Vector<Vector<String>>();
		for (int i = 0 ; i < temp.size(); i++ ){
			Vector<String> temptemp = new Vector<String>();
			temptemp.add(temp.get(i));
			if (temp.get(i).equals("ID")){
			temptemp.add("" + (getLastRowIDFromScenarioDescriptionTable() + 1));	
			} else {
			temptemp.add(templateValues.firstElement().get(i));
			}
			pivot.add(temptemp);
		}
		return pivot;
	}
	
	//get the rows of a specific Scenario for changing entries (for example)
	public static Vector<Vector<String>> getScenarioParameterRows (int scenarioID){
		Vector<String> temp = AccessConnect.getColumnNamesForSelect("SELECT * FROM ScenarioDescriptions");
		Vector<Vector<String>> templateValues = getRowWithIDFromScenarioDescriptionTable(scenarioID);
		Vector<Vector<String>> pivot = new Vector<Vector<String>>();
		for (int i = 0 ; i < temp.size(); i++ ){
			Vector<String> temptemp = new Vector<String>();
			temptemp.add(temp.get(i));
			temptemp.add(templateValues.firstElement().get(i));
			pivot.add(temptemp);
		}
		return pivot;
	}
	//an Empty set, without Tmeplate informations
//	private static Vector<Vector<String>> getScenarioParameterRows (int id){
//		Vector<String> temp = AccessConnect.getColumnNamesForSelect("SELECT * FROM ScenarioDescriptions");
//		Vector<Vector<String>> pivot = new Vector<Vector<String>>();
//		for (int i = 0 ; i < temp.size(); i++ ){
//			Vector<String> temptemp = new Vector<String>();
//			temptemp.add(temp.get(i));
//			if (temp.get(i).equals("ID")){
//			temptemp.add("" + (getLastRowIDFromScenarioDescriptionTable() + 1));	
//			} else {
//			temptemp.add("0");
//			}
//			pivot.add(temptemp);
//		}
//		return pivot;
//	}

	public static Vector<Vector<String>> getHostRows (int Id){
			return AccessConnect.executeSelect("SELECT HostID FROM StartConfigurationNodes WHERE ScenarioID= " + Id);
		}
	
	public static Vector<Vector<String>> getHostRowsForGrafik (int Id){
		return AccessConnect.executeSelect("SELECT xPosition,yPosition,ID FROM HostDescription AS h, StartConfigurationNodes AS s WHERE s.HostID = h.ID AND s.ScenarioID=" + Id + "");
	}
	
	public static Vector<Vector<String>> getHostSelectionRows (){
			return AccessConnect.executeSelect("SELECT * FROM HostDescription");
	}
	
	public static Vector<String> getHostSelectionRowColumnNames(){
		return AccessConnect.getColumnNamesForSelect("SELECT * FROM HostDescription");
	}
	
	public static Vector<String> getActionSelectionList (){
		Vector<Vector<String>> tmp = AccessConnect.executeSelect("SELECT HumanReadableName FROM ActionDescription");
		Vector<String> back = new Vector<String>();
		for (Vector<String> tmptmp : tmp){
			back.add(tmptmp.get(0));
		}
		return back; 
	}
	
//	public static Vector<String> getActionExecutionList (int ScenarioID){
//		Vector<Vector<String>> tmp = AccessConnect.executeSelect("SELECT Action FROM ActionQueue WHERE ScenarioID=" + ScenarioID + " ORDER BY Position ASC");
//		Vector<String> back = new Vector<String>();
//		for (Vector<String> tmptmp : tmp){
//			back.add(tmptmp.get(0));
//		}
//		return back; 		
//	}
	
	public static Vector<Vector<String>> getActionExecutionList (int ScenarioID){
		return AccessConnect.executeSelect("SELECT Action, Affected FROM ActionQueue WHERE ScenarioID=" + ScenarioID + " ORDER BY Position ASC");
	}
	
	//returns only a collection of the ID's 
	public static Vector<String> getAvailableScenarios (){
		Vector<Vector<String>> tmp = AccessConnect.executeSelect("SELECT ID FROM ScenarioDescriptions ORDER BY ID ASC");
		Vector<String> back = new Vector<String>();
		for (Vector<String> tmptmp : tmp){
			back.add(tmptmp.get(0));
		}
		return back; 			
	}
}
